package cn.org.mrliu.ssm.test.module.${modelNameUncapFirst};

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.org.mrliu.ssm.common.test.BaseSpringTestSupport;
import cn.org.mrliu.ssm.common.web.vo.QueryParam;
import cn.org.mrliu.ssm.model.${modelName};
import cn.org.mrliu.ssm.service.${modelName}Service;
import cn.org.mrliu.ssm.util.OrderBy;
import cn.org.mrliu.ssm.util.Pagination;

public class Test${modelName}Service extends BaseSpringTestSupport{
	private static final Logger log = LoggerFactory.getLogger(Test${modelName}Service.class);
	@Resource
	private ${modelName}Service ${modelNameUncapFirst}Service;
	
	@Test
	public void getModelIds() throws Exception {
		List<Integer> ids = ${modelNameUncapFirst}Service.getModelIds();
		log.debug("ids:"+ids);
	}
	
	@Test
	public void getModelIdsByCondition() throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("name", "%name%");
		List<Integer> ids = ${modelNameUncapFirst}Service.getModelIds(map);
		log.debug("ids:"+ids);
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void getModelList() throws Exception {
		List<${modelName}> list = (List<${modelName}>) ${modelNameUncapFirst}Service.getModelList();
		for (${modelName} ${modelNameUncapFirst} : list) {
			log.debug(${modelNameUncapFirst}.toString());
		}
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void getModelListByCondition() throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("name", "%name%");
		List<${modelName}> list = (List<${modelName}>) ${modelNameUncapFirst}Service.getModelList(map);
		for (${modelName} ${modelNameUncapFirst} : list) {
			log.debug(${modelNameUncapFirst}.toString());
		}
	}
	
	@Test
	public void getCount() throws Exception {
		Integer count = ${modelNameUncapFirst}Service.getModelCount();
		log.debug("count:"+count);
	}
	
	@Test
	public void getCountByCondition() throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("name", "%name%");
		Integer count = ${modelNameUncapFirst}Service.getModelCount(map);
		log.debug("count:"+count);
	}
	
	@Test
	public void isExist() throws Exception {
		boolean flag = ${modelNameUncapFirst}Service.isExist("hello");
		log.debug("flag:"+flag);
	}
	
	@Test
	public void isExistByField() throws Exception {
		${modelName} ${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("mock-add");
		boolean flag = ${modelNameUncapFirst}Service.isExist(${modelNameUncapFirst});
		log.debug("flag:"+flag);
	}
	
	@Test
	public void getPagination() throws Exception {
		Pagination pagination = ${modelNameUncapFirst}Service.getPagination(1, 10);
		log.debug("pagination:"+pagination);
	}
	
	@Test
	public void getPaginationByConditionAndOrder() throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("name", "%name%");
		Pagination pagination = ${modelNameUncapFirst}Service.getPagination(1, 10, map, "id",OrderBy.DESC);
		log.debug("pagination:"+pagination);
	}
	
	@Test
	public void getPaginationByQueryParam() throws Exception {
		QueryParam queryParam = new QueryParam();
		queryParam.setPageIndex(1);
		queryParam.setPageSize(20);
		queryParam.setOrderColumn("id");
		queryParam.setOrderBy("desc");
		queryParam.setKeyword("测试");
		Pagination pagination = ${modelNameUncapFirst}Service.getPagination(queryParam);
		System.out.println(pagination);
	}
	
	@Test
	public void getPaginationByQueryParamAndCondition() throws Exception {
		QueryParam queryParam = new QueryParam();
		queryParam.setPageIndex(1);
		queryParam.setPageSize(20);
		queryParam.setOrderColumn("id");
		queryParam.setOrderBy("desc");
		queryParam.setKeyword("name");
		Map<String, Object> map = new HashMap<>();
		map.put("name", "test");
		map.put("keyword", "batchSave");
		Pagination pagination = ${modelNameUncapFirst}Service.getPagination(queryParam,map);
		System.out.println(pagination);
	}
	
	@Test
	public void saveModel() throws Exception {
		${modelName} ${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("test save...");
		Integer id = ${modelNameUncapFirst}Service.saveModel(${modelNameUncapFirst});
		System.out.println(id);
	}
	
	@Test
	public void updateModel() throws Exception {
		${modelName} ${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.setId(1);
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("test update....");
		${modelNameUncapFirst}Service.updateModel(${modelNameUncapFirst});
	}
	
	@Test
	public void saveOrUpdateModelWithSave() throws Exception {
		${modelName} ${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("test saveOrUpdate....");
		${modelNameUncapFirst}Service.saveOrUpdateModel(${modelNameUncapFirst});
	}
	
	@Test
	public void saveOrUpdateModelWithUpdate() throws Exception {
		${modelName} ${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.setId(2);
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("test update...");
		${modelNameUncapFirst}Service.saveOrUpdateModel(${modelNameUncapFirst});
	}
	
	@Test
	public void deleteModel() throws Exception {
		${modelNameUncapFirst}Service.deleteModel(1);
	}
	
	@Test
	public void batchSaveModel() throws Exception {
		List<${modelName}> ${modelNameUncapFirst}s = new ArrayList<>();
		${modelName} ${modelNameUncapFirst} = null;
		${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("batchSave test 1");
		${modelNameUncapFirst}s.add(${modelNameUncapFirst});
		${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("batchSave test 2");
		${modelNameUncapFirst}s.add(${modelNameUncapFirst});
		${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("batchSave test 3");
		${modelNameUncapFirst}s.add(${modelNameUncapFirst});
		${modelNameUncapFirst}Service.batchSaveModel(${modelNameUncapFirst}s);
	}
	
	@Test
	public void batchUpdateModel() throws Exception {
		${modelName} ${modelNameUncapFirst} = new ${modelName}();
		${modelNameUncapFirst}.set${firstColumnNameCapFirst}("batchUpdate test...");
		${modelNameUncapFirst}Service.batchUpdateModel(${modelNameUncapFirst}, new Integer[]{1,2,3,4,5});
	}
	
	@Test
	public void batchDeleteModel() throws Exception {
		${modelNameUncapFirst}Service.batchDeleteModel(new Integer[]{1,2,3});
	}
}
